from osv import osv, fields
from osv.orm import intersect
import tools.sql

class kdvn_vat_invoice_details(osv.osv):
    _name = 'kdvn.vat.invoice.details'
    #_inherit = 'account.invoice'
    _description="KDVN VAT Invoice Details"
    _rec_name="rop_id"
    _auto = False
    _columns={
              'rop_id':fields.many2one('kdvn.request.of.payment','ROP.'),
              'supplier_id':fields.many2one('res.partner','Supplier'),
              'order_id':fields.many2one('purchase.order','Purchase'),
              'project_id':fields.many2one('kdvn.project','Project')
              }

    def init1(self,cr):
        tools.sql.drop_view_if_exists(cr, 'kdvn_vat_invoice_details')
        cr.execute("create or replace view kdvn_vat_invoice_details as \
                        Select \
                            kpri.id,\
                            krop.id as rop_id,\
                            supplier_id,\
                            order_id,\
                            project_id\
                        from \
                            kdvn_purchase_red_invoice kpri \
                        left join \
                            kdvn_request_of_payment_kdvn_purchase_red_invoice_rel kropr on kpri.id=kdvn_purchase_red_invoice_id\
                        left join\
                            kdvn_request_of_payment krop on rop_id  = krop.id where coalesce(krop.id,0)>0")
kdvn_vat_invoice_details()